package com.demo.controller;

import com.demo.common.Result;
import com.demo.mapper.CourseMapper;
import com.demo.pojo.entity.Course;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 课程测试控制器 - 用于调试
 */
@RestController
@RequestMapping("/course-test")
public class CourseTestController {

    private final CourseMapper courseMapper;

    public CourseTestController(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    /**
     * 直接测试数据库查询
     */
    @GetMapping("/direct-query")
    public Result<List<Course>> directQuery() {
        try {
            List<Course> courses = courseMapper.selectList(null);
            return Result.success(courses);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 测试根据ID查询
     */
    @GetMapping("/query-by-id")
    public Result<Course> queryById() {
        try {
            Course course = courseMapper.selectById(1L);
            return Result.success(course);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 测试根据名称查询
     */
    @GetMapping("/query-by-name")
    public Result<Course> queryByName() {
        try {
            Course course = courseMapper.selectByName("Java程序设计");
            return Result.success(course);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}

